package com.ztz.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ztz.services.RepetitionDataServices;
import hibernatebeans.entity.GeneralLedgerEntity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import pers.lt.jdbc.Page;
import pers.lt.tools.StringFacs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RepetitionDataController {

    @Autowired
    private RepetitionDataServices repetitionDataServices;
    //登陆
    @RequestMapping(value = "/login.html")
    public ModelAndView login(){
        ModelAndView modelAndView=new ModelAndView("/login");
        return modelAndView;
    }
    //登陆验证
    @RequestMapping(value = "/indexs.html")
    @ResponseBody
    public Map index(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String ls_result="false";
        Map map = new HashMap();
        String ls_message ="";
        if(username.equals("sys")&&password.equals("manager")){
            ls_result="true";
           // ModelAndView modelAndView=new ModelAndView("/imexcel");
        }else {
            ls_message="账号错误或者密码错误";
            ls_result = "false";
        }
       //ModelAndView modelAndView=new ModelAndView("/imexcel");
        // ModelAndView modelAndView=new ModelAndView("/index/main");
        map.put("message",ls_message);
        map.put("ls_result",ls_result);
        return map;
    }
    //首页
    @RequestMapping(value = "/in.html")
    public ModelAndView in(){
        ModelAndView modelAndView=new ModelAndView("/imexcel");
        return modelAndView;
    }
    //上传
    @RequestMapping(value = "/import.html")
    @ResponseBody
    public Map<String, Object> importExcel(@RequestParam(value="file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(file);
        String result = repetitionDataServices.readExcelFile(file);
        map.put("message", result);
        return map;
    }
    //导出数据查询按钮导出
    @RequestMapping(value = "/export.html")
    @ResponseBody
    public String exportExcel(HttpServletRequest request, HttpServletResponse response){
        String fileName = null;
        try {
            String line_No=request.getParameter("lineNo");
            String weldNo=request.getParameter("weldNo");
            String flag=request.getParameter("flag");
            fileName = repetitionDataServices.exportExcel(request,line_No,weldNo,flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "upload/" + fileName;
    }
    //删除全部
    @RequestMapping(value = "/deleteAll.html")
    @ResponseBody
    public void deleteAll(HttpServletRequest request, HttpServletResponse response){
        String fileName = null;
        try {
            repetitionDataServices.deleteAll(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //数据加载
    @RequestMapping(value = "/repetitiondatalist.html")
    @ResponseBody
    public void repetitionDataList(Page page, HttpServletResponse response, HttpServletRequest request) throws JsonProcessingException {
        Page repage = repetitionDataServices.repetitionDataList(page);
        int li_total = repage.getPagetotal();
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(fmt);
        String json = mapper.writeValueAsString(repage.getList());
        // JSONArray jsonArray = JSONArray.fromObject(repage.getList());
        //String json = jsonArray.toString();
        String ls_result = "{\"total\":" + li_total + ",\"rows\":" + json + "}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(ls_result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //删除数据
    @RequestMapping(value = "/sjDelete.html")
    public void sjDelete(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String ids=request.getParameter("ids");
        repetitionDataServices.sjDelete(ids);
    }
    //不重复数据查询
    @RequestMapping(value = "/querts.html")
    @ResponseBody
    public void querts(Page page, HttpServletResponse response, HttpServletRequest request, GeneralLedgerEntity generalLedgerEntity) throws JsonProcessingException {

       //这里取jsp传来的值在实体类中取和在request.getParameter取是一样的,都可以拿到
//        System.out.println(generalLedgerEntity.getDrawingNo());
//        System.out.println(generalLedgerEntity.getWeldNo());
//        System.out.println(request.getParameter("drawingNo"));

        Page repage = repetitionDataServices.querys(page,generalLedgerEntity);
        int li_total = repage.getPagetotal();
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(fmt);
        String json = mapper.writeValueAsString(repage.getList());
        // JSONArray jsonArray = JSONArray.fromObject(repage.getList());
        //String json = jsonArray.toString();
        String ls_result = "{\"total\":" + li_total + ",\"rows\":" + json + "}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(ls_result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //条件查询
    @RequestMapping(value = "/quertlist.html")
    @ResponseBody
    public void quertlist(Page page, HttpServletResponse response, HttpServletRequest request) throws JsonProcessingException {

        //这里取jsp传来的值在实体类中取和在request.getParameter取是一样的,都可以拿到
//        System.out.println(generalLedgerEntity.getDrawingNo());
//        System.out.println(generalLedgerEntity.getWeldNo());
//        System.out.println(request.getParameter("drawingNo"));
        Page repage = repetitionDataServices.querylist(page);
        int li_total = repage.getPagetotal();
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(fmt);
        String json = mapper.writeValueAsString(repage.getList());
        // JSONArray jsonArray = JSONArray.fromObject(repage.getList());
        //String json = jsonArray.toString();
        String ls_result = "{\"total\":" + li_total + ",\"rows\":" + json + "}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(ls_result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    /**
     * ----------------------------------------------------------------------------------------------------
     * @return
     */
    //导入查询首页
    @RequestMapping(value = "/importlist.html")
    @ResponseBody
    public ModelAndView indeximport(){

        ModelAndView modelAndView=new ModelAndView("/importexlist");
        return modelAndView;
    }
    //导出上传保存
    @RequestMapping(value = "/drimportlist.html")
    @ResponseBody
    public Map<String, Object> importExcellist(@RequestParam(value="drfile",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        //删除临时表
        repetitionDataServices.del();
        String result = repetitionDataServices.readExcelFilelist(file);
        map.put("message", result);
        return map;
    }
    //根据导入数据导出
    @RequestMapping(value = "/exportlist.html")
    @ResponseBody
    public String exportExcellist(HttpServletRequest request, HttpServletResponse response){
        String fileName = null;
        try {
            fileName = repetitionDataServices.exportExcellist(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "upload/" + fileName;
    }
}